package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import open.dto.tiktok.TiktokPackageDs;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建包裹
 * <p>
 * cy.tiktok.order.package.create
 * /fulfillment/202309/packages (创建发货箱) https://partner.tiktokshop.com/docv2/page/650aa132bace3e02b75d40d8?external_id=650aa132bace3e02b75d40d8
 * </p>
 *
 * @author gywang
 * @since 2024-07-26
 */
@Slf4j
@Service
public class TiktokOrderPackageCreateService extends AbstractBaseTiktokApiHandler<TiktokPackageDs> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.ORDER_PACKAGE_CREATE;
    }

    @Override
    public String getLocalApiMethod() {
        return PlatformApiConstant.InboundInterface.TIKTOK_ORDER_PACKAGE_CREATE;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, TiktokPackageDs ds) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Map<String, Object> reqBody = new HashMap<>();
        reqBody.put("order_id", ds.getSourceOrderCode());
        reqBody.put("order_line_item_ids", ds.getSourceLineNums());
        Map<String, Object> weight = new HashMap<>();
        weight.put("value", ds.getWeight());
        weight.put("unit", ds.getWeightUnit());
        reqBody.put("weight", weight);
        Map<String, Object> dimension = new HashMap<>();
        dimension.put("length", ds.getLength());
        dimension.put("width", ds.getWidth());
        dimension.put("height", ds.getHeight());
        dimension.put("unit", ds.getDimensionUnit());
        reqBody.put("dimension", dimension);
        reqBody.put("shipping_service_id", ds.getShippingServiceDs().getShippingServiceId());

        return this.executeTiktokApi(getDefaultUrl(platformApiParamDTO.getStoreConfig()), platformApiParamDTO.getStoreConfig(), params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);
    }
}
